package com.RPGDice;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.RPGDice.util.Roll;
import com.RPGDice.util.RollSet;
import com.RPGDice.util.RollableDice;
import com.RPGDice.util.Settings;
import com.criticalritrpg.R;

public class RollActivity extends Activity implements OnClickListener {

	Button rollButton, clearScreenButton;
	TextView resultsTv, sumTv, successTv;
	String resultsStr = "";
	ScrollView sv;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.roll);
		sv = (ScrollView) findViewById(R.id.scrollview);
		rollButton = (Button) findViewById(R.id.roll_dices_button);
		clearScreenButton = (Button) findViewById(R.id.clear_screen_button);

		rollButton.setOnClickListener(this);
		clearScreenButton.setOnClickListener(this);

		resultsTv = (TextView) findViewById(R.id.results_tv);
		sumTv = (TextView) findViewById(R.id.sum_tv);
		successTv = (TextView) findViewById(R.id.success_tv);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.roll_dices_button:
			rollDices();
			break;
		case R.id.clear_screen_button:
			resultsStr = "";
			resultsTv.setText(resultsStr);
			sumTv.setText("");
			successTv.setText("");
			break;
		}
	}

	private void rollDices() {
		// String curResultsStr = "";
		int sum = 0;
		resultsStr = "";
		List<RollSet> results = Roll.rollDices();
		int rollSetCount = 0;
		for (RollSet curResult : results) {
			rollSetCount++;
			String rollText = "<font color=" + "#000000" + ">" + "&nbsp;&nbsp;>>"
					+ "<b> " + getString(R.string.current_roll_number) + " "
					+ rollSetCount + " </b>" + "</font>" + "<br />";
			// String newText = "";
			for (RollableDice curRollableDice : curResult.getRollableDices()) {
				int[] curResults = curRollableDice.getRollValues();
				String diceText = "";
				for (int i = 0; i < curResults.length; i++)
					diceText += (i < curResults.length - 1) ? (curResults[i] + ", ")
							: String.valueOf(curResults[i]);
				diceText += " = " + curRollableDice.getRollResult();
				sum += curRollableDice.getRollResult();
				rollText += "<font color=" + curRollableDice.getColor() + ">"
						+ "<b> " + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(" + curRollableDice.getDiceName()
						+ ") " + " </b>" + diceText + "</font>" + "<br />";

			}
			resultsStr += rollText;
		}
		
		Settings.addToHistory(resultsStr, this);
		
		resultsTv.setText(Html.fromHtml(resultsStr));
		scrollToEnd(0);

		if (Settings.getShowSum()) {
			sumTv.setText(getString(R.string.sum_result) + " " + sum);
			sumTv.setVisibility(View.VISIBLE);
		} else
			sumTv.setVisibility(View.INVISIBLE);
		System.out.println("SUM = " + sum + " lower: " + Settings.getSuccessMargin()[0] + " upper: " + Settings.getSuccessMargin()[1]);
		if (Settings.getShowSuccess() && Settings.checkValidMargins()) {
			if (sum >= Settings.getSuccessMargin()[0]
					&& sum <= Settings.getSuccessMargin()[1])
				successTv.setText(getString(R.string.result_says) + ": "
						+ getString(R.string.success));
			else
				successTv.setText(getString(R.string.result_says) + ": "
						+ getString(R.string.failure));
			successTv.setVisibility(View.VISIBLE);
		} else
			successTv.setVisibility(View.INVISIBLE);
	}

	private void scrollToEnd(int delay) {
		sv.postDelayed(new Runnable() {

			public void run() {
				sv.fullScroll(View.FOCUS_DOWN);

			}
		}, delay);
	}

}
